<template>
  <div>
    <!-- <el-button type="success">成功按钮</el-button> -->
    <div class="login">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>请登录</span>
          <el-button style="float: right; padding: 3px 0" type="text">忘记密码</el-button>
        </div>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="name">
            <el-input v-model="ruleForm.name" autofocus></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input show-password v-model="ruleForm.password" v-on:keyup.enter="submitForm('ruleForm')"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import qs from "qs";
import {IP} from "../util/util";
import { Card, Button, Form, Input, FormItem, Message } from "element-ui";
Vue.use(Card);
Vue.use(Button);
Vue.use(Form);
Vue.use(Input);
Vue.use(FormItem);
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        password: ""
      },
      rules: {
        name: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 12, message: "长度在 3 到 12 个字符", trigger: "blur" }
        ]
      }
    };
  },
  component: {
    // Button
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //   alert("submit!");
          this.login();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    login() {
      this.$ajax({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        url: "http://"+IP+"/php/login.php",
        method: "post",
        data: qs.stringify({
          name: this.ruleForm.name,
          password: this.ruleForm.password
        })
      }).then(res => {
        // console.log(res.data);return;
        if (res.data.success) {
          sessionStorage.setItem("logininfo", JSON.stringify(res.data));
          Message.success("登陆成功");
          this.$router.push('/home');
        } else {
          Message.error("账号或密码错误");
        }
        console.log(res);
      });
    }
  }
};
</script>

<style>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 40vw;
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 150px;
}
</style>
